<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleExport({})" v-auth="'shop:apply:soReturnApply:view'">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ code: record.code })">
          {{ record.code }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsShopApplySoReturnApplyList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { soReturnApplyDelete, soReturnApplyExport } from '/@/api/shop/apply/soReturnApply';
  import { basUserCusTreeData } from '/@/api/bas/cus/basUserCus';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl2 } from '/@/utils/file/download';
  import { BasicModal, useModal } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';

  const { t } = useI18n('shop.apply.soReturnApply');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('退货订单明细管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('单据号'),
        field: 'soReturnApply.code',
        component: 'Input',
      },
      {
        label: t('单据日期'),
        field: 'soReturnApply.ddate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('业务类型'),
        field: 'soReturnApply.cbustype',
        component: 'Select',
        componentProps: {
          dictType: 'bus_type',
          allowClear: true,
        },
      },
      {
        label: t('客户编码'),
        field: 'soReturnApply.cusCode',
        component: 'Input',
      },
      {
        label: t('客户名称'),
        field: 'soReturnApply.basCus.name',
        component: 'Input',
      },
      {
        label: t('核发店面'),
        field: 'soReturnApply.hfdmName',
        component: 'Input',
      },
      {
        label: t('是否首批'),
        field: 'soReturnApply.bsp',
        component: 'Select',
        componentProps: {
          dictType: 'bus_is_first',
          allowClear: true,
        },
      },
      {
        label: t('物流公司'),
        field: 'soReturnApply.wlComp',
        component: 'Input',
      },
      {
        label: t('物流单号'),
        field: 'soReturnApply.wlNo',
        component: 'Input',
      },
      {
        label: t('审核人'),
        field: 'soReturnApply.auditor.userName',
        component: 'Input',
      },
      {
        label: t('审核日期'),
        field: 'soReturnApply.auditDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('审核状态'),
        field: 'soReturnApply.auditStatus',
        component: 'Select',
        componentProps: {
          dictType: 'so_return_audit_status',
          allowClear: true,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('状态'),
      dataIndex: 'soReturnApply.auditStatus',
      key: 'apply.audit_status',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'so_return_audit_status',
    },
    {
      title: t('单据号'),
      dataIndex: 'soReturnApply.code',
      key: 'apply.code',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('单据日期'),
      dataIndex: 'soReturnApply.ddate',
      key: 'apply.ddate',
      sorter: true,
      width: 120,
      align: 'center',
    },
    {
      title: t('业务类型'),
      dataIndex: 'soReturnApply.cbustype',
      key: 'apply.cbustype',
      sorter: true,
      width: 120,
      align: 'center',
      dictType: 'bus_type',
    },
    {
      title: t('客户编码'),
      dataIndex: 'soReturnApply.cusCode',
      key: 'apply.cusCode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('客户名称'),
      dataIndex: 'soReturnApply.basCus.name',
      key: 'cus.name',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('销售部门'),
      dataIndex: 'soReturnApply.deptName',
      key: 'apply.deptName',
      sorter: true,
      width: 200,
      align: 'left',
      ifShow: false,
    },
    {
      title: t('核发店面'),
      dataIndex: 'soReturnApply.hfdmName',
      key: 'apply.hfdm_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('是否首批'),
      dataIndex: 'soReturnApply.bsp',
      key: 'apply.bsp',
      sorter: true,
      width: 100,
      align: 'center',
      dicType: 'bus_is_first',
    },
    {
      title: t('是否包邮'),
      dataIndex: 'soReturnApply.bby',
      key: 'apply.bby',
      sorter: true,
      width: 100,
      align: 'center',
      dicType: 'bus_is_free',
    },
    {
      title: t('物流公司'),
      dataIndex: 'soReturnApply.wlComp',
      key: 'apply.wl_comp',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('物流单号'),
      dataIndex: 'soReturnApply.wlNo',
      key: 'apply.wl_no',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('余额'),
      dataIndex: 'soReturnApply.zhMoney',
      key: 'apply.zh_money',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('促销费用余额'),
      dataIndex: 'soReturnApply.cxMoney',
      key: 'apply.cx_money',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('经销商返利余额'),
      dataIndex: 'soReturnApply.flMoney',
      key: 'apply.fl_money',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('使用账户金额'),
      dataIndex: 'soReturnApply.useZhMoney',
      key: 'apply.use_zh_money',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('促销返利金额'),
      dataIndex: 'soReturnApply.useFlMoney',
      key: 'apply.use_fl_money',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('使用促销金额'),
      dataIndex: 'soReturnApply.useCxMoney',
      key: 'apply.use_cx_money',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('审核人'),
      dataIndex: 'soReturnApply.auditor.userName',
      key: 'apply.user_name',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('审核日期'),
      dataIndex: 'soReturnApply.auditDate',
      key: 'apply.audit_date',
      sorter: true,
      width: 120,
      align: 'center',
    },
    {
      title: t('仓库编码'),
      dataIndex: 'whcode',
      key: 'a.whcode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('仓库名称'),
      dataIndex: 'basWare.whname',
      key: 'ware.whname',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('存货编码'),
      dataIndex: 'cinvcode',
      key: 'a.cinvcode',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('存货名称'),
      dataIndex: 'basInv.cinvname',
      key: 'inv.cinvname',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('换算率'),
      dataIndex: 'ichangerate',
      key: 'a.ichangerate',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('申请数量'),
      dataIndex: 'applyIqty',
      key: 'a.apply_iqty',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('申请件数'),
      dataIndex: 'applyInum',
      key: 'a.apply_inum',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('含税单价'),
      dataIndex: 'itaxunitprice',
      key: 'a.itaxunitprice',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('价税合计'),
      dataIndex: 'isum',
      key: 'a.isum',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('税率'),
      dataIndex: 'itaxrate',
      key: 'a.itaxrate',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('扣率'),
      dataIndex: 'klRate',
      key: 'a.kl_rate',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('折扣额'),
      dataIndex: 'idiscount',
      key: 'a.idiscount',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('是否赠品'),
      dataIndex: 'bgift',
      key: 'a.bgift',
      sorter: true,
      width: 120,
      align: 'center',
      dictType: 'sys_yes_no',
    },
    {
      title: t('促销金额'),
      dataIndex: 'cxMoney',
      key: 'a.cx_money',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('返利金额'),
      dataIndex: 'flMoney',
      key: 'a.fl_money',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('业绩金额'),
      dataIndex: 'yjMoney',
      key: 'a.yj_money',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('退回理由'),
      dataIndex: 'soReturnApply.remarks',
      key: 'apply.remarks',
      sorter: true,
      width: 200,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRows }] = useTable({
    api: soReturnApplyExport,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    //actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  function handleSuccess() {
    reload();
  }

  // 导出
  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl2({
      url: ctxAdminPath + '/shop/apply/soReturnApply/exportData',
      params: getForm().getFieldsValue(),
    });
  }
</script>
